﻿Imports System.Timers
Imports MyIPBssLogic
Imports MyIPBssLogic.AppConfig
Imports System.IO

Public Class MyIpLocalSrv

    Private Shared TimerStartApp As System.Timers.Timer
    Private Shared TimerCheckIP As System.Timers.Timer
    Private Shared Conf As Configuration

    Private Shared FileConf As String = Path.Combine( _
                                    My.Application.Info.DirectoryPath, _
                                      "Conf.xml")

    Protected Overrides Sub OnStart(ByVal args() As String)

        'Only when debug to take time to attach the process with VS
        'System.Threading.Thread.Sleep(10)

        TimerStartApp = New System.Timers.Timer(10000)
        ' Hook up the Elapsed event for the timer.
        AddHandler TimerStartApp.Elapsed, AddressOf OnTimedStartApp
        TimerStartApp.Start()


    End Sub

    Protected Overrides Sub OnStop()
        ' Agregue el código aquí para realizar cualquier anulación necesaria para detener el servicio.
    End Sub

    ' Specify what you want to happen when the Elapsed event is 
    ' raised.
    Private Shared Sub OnTimedStartApp(ByVal source As Object, ByVal e As ElapsedEventArgs)

        TimerStartApp.Stop()

        Try

            Conf = ConfMngmnt.Load(FileConf)

            If Conf.ConfLoaded Then

                Try

                    If Conf.MyIPAppConfigData.SendMailAlwaysOnStart Then

                        Dim IPNow As String = NetLogicBuss.GetIpAdress(Conf.MyIPAppConfigData.URLWebServLocateIP)
                        Conf.MyIPMailConf.Email.Body = "The actual IP is " & IPNow

                        Dim eMailer As New MailSender(Conf.MyIPMailConf)

                        eMailer.SendMessage()

                    End If

                Catch ex As Exception



                End Try

                TimerCheckIP = New System.Timers.Timer(Conf.MyIPAppConfigData.TimeToCheckChange)

                AddHandler TimerCheckIP.Elapsed, AddressOf OnTimedCheckIP

                TimerCheckIP.Interval = Conf.MyIPAppConfigData.TimeToCheckChange
                TimerCheckIP.Enabled = True

                TimerCheckIP.Start()

                Return

            End If

        Catch ex As Exception

        End Try

        TimerStartApp.Start()

    End Sub

    ' Specify what you want to happen when the Elapsed event is 
    ' raised.
    Private Shared Sub OnTimedCheckIP(ByVal source As Object, ByVal e As ElapsedEventArgs)

        TimerCheckIP.Stop()

        Try
            Dim IPNow As String = NetLogicBuss.GetIpAdress(Conf.MyIPAppConfigData.URLWebServLocateIP)
            If Conf.MyIPAppConfigData.LastIP <> IPNow Then

                Conf.MyIPMailConf.Email.Body = "The new IP is " & IPNow

                Dim eMailer As New MailSender(Conf.MyIPMailConf)

                If eMailer.SendMessage() = True Then

                    Conf.MyIPAppConfigData.LastIP = IPNow
                    ConfMngmnt.Save(FileConf, Conf)

                End If

            End If

        Catch ex As Exception

        End Try

        TimerCheckIP.Start()

    End Sub

End Class
